package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://paper.dzwww.com/ 大众日报
 * @author Persh 
 */
public class DzwwwNewsPaperAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://paper.dzwww.com/dzrb/content/[0-9]{8}/Articel[0-9]*MT.htm",
				"http://paper.dzwww.com/ncdz/content/[0-9]{8}/Articel[0-9]*MT.htm",
				"http://shrb.qlwb.com.cn/shrb/content/[0-9]{8}/ArticelA.*.htm",
				"http://paper.dzwww.com/jjdb/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://paper.dzwww.com/sdfzb/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://paper.dzwww.com/dzrbhwbbm/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://paper.dzwww.com/sdqnb/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://paper.dzwww.com/dzrbhwbnm/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://paper.dzwww.com/xcb/data/[0-9]{8}/html/[0-9]*/content_[0-9]*.html",
				"http://epaper.rznews.cn/shtml/rzrb/[0-9]{8}/[0-9].*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
						
		newspaper.setUrl(url);		
		String title = null;
		String content = null;
		Long date = null;
		Document doc = Jsoup.parse(html);
		
		title =doc.select("html body div#wrapper div#maincon div.clearfix div#news-con.column_1of2 div.top h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("html body div#wrapper div#content div.main div#news-header h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("html body div.wrap div.content div.xilan div.right div.neirong1 div.wenzi h1").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("html body table tbody tr td table.border-grey tbody tr td#DivDisplay.f-14 div.f-20 strong").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("td.font01").text();
					}
				}
			}
		}
		date=DateUtils.matchDate(url);
		
		content =HtmlCleaner.getContentHtml(url,doc.select("span#contenttext"));
		if(content==null || content.trim().length()==0){
			content =HtmlCleaner.getContentHtml(url,doc.select("div#content"));
		}
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://paper.dzwww.com/dzrb/content/[0-9]{8}/Page[0-9]*NU.htm",
				"http://paper.dzwww.com/ncdz/content/[0-9]{8}/Page[0-9]*NU.htm",
				"http://shrb.qlwb.com.cn/shrb/content/[0-9]{8}/PageA[0-9]*SA.htm",
				"http://paper.dzwww.com/jjdb/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://paper.dzwww.com/sdfzb/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://paper.dzwww.com/dzrbhwbbm/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://paper.dzwww.com/dzrbhwbnm/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://paper.dzwww.com/sdqnb/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://paper.dzwww.com/xcb/data/[0-9]{8}/html/[0-9]*/partindex.html",
				"http://epaper.rznews.cn/shtml/.*/[0-9]{8}/v.*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	

	

	
	
}
